home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue54 / System / ThreadTest / FileSysThread.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-01-07  |  2.3 KB  |  79 lines

  1. unit FileSysThread;
  2.  
  3. interface
  4.  
  5. uses
  6.     Windows, SysUtils, Classes, comctrls;
  7.  
  8. type
  9.     TFileSysNotifyThread = class(TThread)
  10.     private
  11.         ErrCode: Integer;
  12.         KillAddress: PInteger;
  13.         NotifyHandle: THandle;
  14.         WatchPath: String;
  15.         WatchMask: Integer;
  16.         procedure SignalFileNotification;
  17.     protected
  18.         procedure Execute; override;
  19.     public
  20.         constructor Create (const AWatchPath: String; AWatchMask: Integer; var Myself: TFileSysNotifyThread);
  21.         destructor Destroy; override;
  22.     end;
  23.  
  24. implementation
  25.  
  26. uses Dialogs;
  27.  
  28. constructor TFileSysNotifyThread.Create (const AWatchPath: String; AWatchMask: Integer; var Myself: TFileSysNotifyThread);
  29. begin
  30.     // Thread is created in suspended state
  31.     Inherited Create (True);
  32.     // Save various stuff for later
  33.     WatchPath := AWatchPath;
  34.     WatchMask := AWatchMask;
  35.     KillAddress := Addr (Myself);
  36.     // Priority is lower than normal
  37.     Priority := tpLower;
  38.     // Set thread to free itself when terminated
  39.     FreeOnTerminate := True;
  40.     // Let slip the dogs of war...
  41.     Suspended := False;
  42. end;
  43.  
  44. destructor TFileSysNotifyThread.Destroy;
  45. begin
  46.     if NotifyHandle <> THandle (-1) then FindCloseChangeNotification (NotifyHandle);
  47.     Inherited Destroy;
  48.     KillAddress^ := 0;  // Very last thing we do....
  49. end;
  50.  
  51. procedure TFileSysNotifyThread.Execute;
  52. begin
  53.     // Create the synchronisation object
  54.     NotifyHandle := FindFirstChangeNotification (PChar (WatchPath), False, WatchMask);
  55.     // No synchronisation object, no comment...
  56.     if NotifyHandle <> THandle (-1) then while not Terminated do begin
  57.         ErrCode := WaitForSingleObject (NotifyHandle, 250);
  58.         // Was it a timeout, or something more interesting ?
  59.         case ErrCode of
  60.              Wait_Timeout:   // Just a timeout -- ignore it....
  61.                  ;
  62.              Wait_Object_0:  // We've got a valid change notification
  63.                  begin
  64.                      Synchronize (SignalFileNotification);
  65.                      FindNextChangeNotification (NotifyHandle);
  66.                  end;
  67.  
  68.              else ;            // Something deeply bad has happened....
  69.         end;
  70.     end;
  71. end;
  72.  
  73. procedure TFileSysNotifyThread.SignalFileNotification;
  74. begin
  75.     ShowMessage ('Something''s changed!');
  76. end;
  77.  
  78. end.
  79.